package com.woniuxy.reader.controller;


import com.woniuxy.reader.entity.Chapter;
import com.woniuxy.reader.service.ChapterService;
import com.woniuxy.result.ResponseResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zh_o
 * @since 2020-10-12
 */
@RestController
@RequestMapping("/chapter")
public class ChapterController {
    @Resource
    private ChapterService chapterService;
    @PostMapping
    public ResponseResult updateChapter(Chapter chapter){
        chapter.setWordNumber(chapter.getContent().length());
        System.out.println(chapter.getWordNumber());
        return ResponseResult.success(chapterService.save(chapter));
    }

    /**
     * 获取章节页面初始化信息
     * @param chapterId
     * @return
     */
    @GetMapping("{chapterId}")
    public Object initChapterInfo(@PathVariable("chapterId") Integer chapterId) {
        return ResponseResult.success(chapterService.getChapterInfo(chapterId));
    }

}

